package com.fosu.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.fosu.common.ResponseResult;
import com.fosu.dao.CampusDao;
import com.fosu.domain.Campus;
import com.fosu.domain.ProductCategories;
import com.fosu.service.CampusService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Desription
 *
 * @author manno
 * @date 2020/10/22
 */
@Service
public class CampusServiceImpl implements CampusService
{
    @Autowired
    CampusDao campusDao;

    @Override
    public ResponseResult findAll( HttpServletResponse resp) throws Exception
    {
        /**获得校区集合*/
        List<Campus> campusList =  campusDao.findAll();
        /**转为json格式*/
        String json = JSON.toJSONString(campusList);
        /**处理别名*/
        json = json.replaceAll("campusId","optionsId");
        json = json.replaceAll("campusName","optionsName");
        System.out.println(json);
        /**用JSONArray封装*/
        JSONArray jsonArray = (JSONArray) JSON.parse(json);
        /**JSONArray返回标准的json*/
        if(campusList.size() != 0)
        {
            return ResponseResult.build(resp,200,10011,"获取校区列表成功",jsonArray.toArray());
        }
        return ResponseResult.build(resp,500,10012,"获取校区列表失败",jsonArray.toArray());
    }

}
